﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Recipe
{
    public class Recipie:IRecipe
    {
        public void AddRecipe(RecipeData Recipe, ref Model.NutriLeafDBEntities context)
        {
            //..doubt
            foreach (Model.RecipeStep  steps in Recipe.recipeSteps.ToList())
            {
                context.RecipeSteps.AddObject(steps);
                context.SaveChanges();
            }
            foreach (Model.RecipeImage images in Recipe.recipeImages.ToList())
            {
                context.RecipeImages.AddObject(images);
                context.SaveChanges();
            }
            foreach (Model.FoodIngredient grids in Recipe.recipeIngredients.ToArray())
            {
                context.FoodIngredients.AddObject(grids);
                context.SaveChanges();
            }
            }


        public void AddRecipe(string RecipeName, int categoryId, string description, int userId, bool approved, ref Model.NutriLeafDBEntities context)
        {
            Model.Recipe piece = new Model.Recipe();
            piece.RecipeName = RecipeName;
            piece.RecipeDescription = description;
            piece.UserID = userId;
            piece.Approved = approved;
            context.Recipes.AddObject(piece);
            context.SaveChanges();
        }

        public List<Model.Recipe> SuggestRecipes(int userId, ref Model.NutriLeafDBEntities context)
        {
            return context.Recipes.Where(p => p.UserID == userId).ToList() ;
        }

        public void RemoveRecipe(int RecipeId, ref Model.NutriLeafDBEntities context)
        {
            if (context.Recipes.Where(p => p.RecipeID == RecipeId).Count() != 0) {
                context.DeleteObject(context.Recipes.Where(p => p.RecipeID==RecipeId).First());
                context.SaveChanges();
            }
        }

        public void UpdateRecipe(RecipeData RecipeData, ref Model.NutriLeafDBEntities context)
        {
            //requires updaete
            throw new NotImplementedException();
        }

        public void UpdateRecipe(Model.Recipe Recipe, ref Model.NutriLeafDBEntities context)
        {
            //requires update
            throw new NotImplementedException();
        }

        public RecipeData GetRecipeData(int RecipeId, ref Model.NutriLeafDBEntities context)
        {  
            //doubt;
            return new RecipeData();
        }

        public List<General.StringIdPair> GetRecipeNames(string startsWith, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void ApproveRecipe(int recipeId, ref Model.NutriLeafDBEntities context)
        {
         //requires update4.
        }

        public List<Model.Recipe> GetAllNotApprovedRecipe(int categoryId, ref Model.NutriLeafDBEntities context)
        {
            return context.Recipes.Where(p => p.RecipeCategoryID == categoryId).ToList();
        }
    }
}